//
// OpenTemplate
// Copyright (c) 2006
// by OpenArrow Software ( http://www.openarrow.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Text;
using OpenArrow.Templating.Properties;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace OpenArrow.Templating
{
    /// <summary>
    /// Exception thrown when problems that occur during an action require the action to cease
    /// </summary>
    [Serializable()]
    public class ActionProblemsException : Exception
    {
        private ProblemCollection problems;

        /// <summary>
        /// The problems that caused the exception, contains all problems since the action started
        /// </summary>
        public ProblemCollection Problems
        {
            get { return problems; }
        }

        /// <summary>
        /// Constructs a new <see cref="ActionProblemsException"/> object.
        /// </summary>
        public ActionProblemsException() : this(Resources.Error_CompilationErrors) { }

        /// <summary>
        /// Constructs a new <see cref="ActionProblemsException"/> object.
        /// </summary>
        /// <param name="problems">The problems that caused the exception</param>
        public ActionProblemsException(ProblemCollection problems) : this(Resources.Error_CompilationErrors, problems) { }

        /// <summary>
        /// Constructs a new <see cref="ActionProblemsException"/> object.
        /// </summary>
        /// <param name="message">A message to associate with the exception</param>
        public ActionProblemsException(string message) : base(message) { }

        /// <summary>
        /// Constructs a new <see cref="ActionProblemsException"/> object.
        /// </summary>
        /// <param name="message">A message to associate with the exception</param>
        /// <param name="problems">The problems that caused the exception</param>
        public ActionProblemsException(string message, ProblemCollection problems)
            : this(message)
        {
            this.problems = problems;
        }

        /// <summary>
        /// Constructs a new <see cref="ActionProblemsException"/> object.
        /// </summary>
        /// <param name="message">A message to associate with the exception</param>
        /// <param name="innerException">An "inner" exception that caused this exception</param>
        public ActionProblemsException(string message, Exception innerException) : base(message, innerException) { }

        /// <summary>
        /// Constructs a new <see cref="ActionProblemsException"/> object.
        /// </summary>
        /// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown</param>
        /// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination</param>
        /// <exception cref="ArgumentNullException"><paramref name="info"/> is <see langword="null"/></exception>
        protected ActionProblemsException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            if(info == null)
                throw new ArgumentNullException(String.Format(Resources.Exception_ArgIsNull, "info"), "info");

            this.problems = (ProblemCollection)info.GetValue("problems", typeof(ProblemCollection));
        }

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="SerializationInfo"/> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown</param>
        /// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination</param>
        /// <exception cref="ArgumentNullException"><paramref name="info"/> is <see langword="null"/></exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.ArgumentNullException.#ctor(System.String,System.String)"), SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        {
            if(info == null)
                throw new ArgumentNullException(String.Format(Resources.Exception_ArgIsNull, "info"), "info");
            
            base.GetObjectData(info, context);
            info.AddValue("problems", this.Problems);
        }
    }
}
